package com.example;

import com.google.common.collect.Lists;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Create By Liuqz
 * @date 2018/11/15 14:34
 */
public class SimpleStream {


    public static void main(String[] args) {

        List<Dish> menu = Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH));

//        List<String> result = getDishNameByStream(menu);
//        System.out.println(result);

//        menu.stream();
//        menu.stream().forEach(System.out::print);
//        menu.stream().forEach(System.out::print);
//
//        List<String> result = menu.stream().filter(d -> {
//            System.out.println("filtering -->" + d.getName());
//            return d.getCalories() > 200;
//        }).map(d -> {
//            System.out.println("map -->" + d.getName());
//            return d.getName();
//        }).limit(3).collect(Collectors.toList());

        int sum = menu.stream().mapToInt(Dish::getCalories).sum();
        menu.stream().mapToInt(Dish::getCalories).max();
        System.out.println(sum);

    }








    private static List<String> getDishNameByStream(List<Dish> menu) {
        return menu.stream().filter(dish -> dish.getCalories() < 400)
                .sorted((Comparator.comparing(Dish::getCalories))).map(Dish::getName).collect(Collectors.toList());
    }

    private static List<String> getDishNamesByCollections(List<Dish> menu) {
        List<Dish> lowCalories = new ArrayList<>();

        for (Dish dish : menu) {
            if (dish.getCalories() < 400) {
                lowCalories.add(dish);
            }
        }

        //sort
        Collections.sort(lowCalories, (d1, d2) -> Integer.compare(d1.getCalories(), d2.getCalories()));

        List<String> dishName = Lists.newArrayList();
        for (Dish lowCalory : lowCalories) {
            dishName.add(lowCalory.getName());
        }
        return dishName;
    }
}
